package moduloIndexacion.frames;

import entidades.Extension;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import java.awt.GridLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.border.BevelBorder;
import javax.swing.ListSelectionModel;

import moduloIndexacion.gestores.GestorIndexacion;

public class FrameExtensiones extends JFrame {

	private JPanel contentPane;
	private GestorIndexacion gestor;
	private final DefaultListModel<Extension> modeloSelecc = new DefaultListModel<Extension>();
	private final DefaultListModel<Extension> modeloPerm = new DefaultListModel<Extension>();
	
	/**
	 * Create the frame.
	 */
	public FrameExtensiones( GestorIndexacion gestor) {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(FrameExtensiones.class.getResource("/resources/ChewieIcon.jpg")));
		this.gestor = gestor;
		initialize();
		if(gestor.getExtensionesSelecc() != null){
			Extension[] selecc = gestor.getExtensionesSelecc();
			for (int i = 0; i< gestor.getExtensionesSelecc().length; i++){
				modeloSelecc.addElement(selecc[i]);
				modeloPerm.removeElement(selecc[i]);
			}
			
		}
	}
	
	public void initialize(){
		setTitle("Chewie - Motor de Busqueda de Informaci\u00F3n");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 580, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panelMedio = new JPanel();
		contentPane.add(panelMedio, BorderLayout.CENTER);
		
		final JList<Extension> listPermitidos = new JList<Extension>();
		listPermitidos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listPermitidos.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		listPermitidos.setToolTipText("Formatos Perimitidos por el Sistema");
		
		listPermitidos.setModel(modeloPerm);
		settearListaSelecc(modeloPerm);
		
		
		final JList<Extension> listSelecc = new JList<Extension>();
		listSelecc.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listSelecc.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		listSelecc.setToolTipText("Formatos Seleccionados");
		
		listSelecc.setModel(modeloSelecc);
		
		listPermitidos.setSelectedIndex(0);
		listSelecc.setSelectedIndex(0);
		
		JButton btnPonerTodo = new JButton(">>");
		btnPonerTodo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int size = modeloPerm.getSize();
				Extension[] array = new Extension[size];
				modeloPerm.copyInto(array);
				for(int i = 0; i<array.length;i++){
					modeloSelecc.addElement(array[i]);
				}
				modeloPerm.clear();
				listPermitidos.setSelectedIndex(0);
				listSelecc.setSelectedIndex(0);
			}
		});
		
		JButton btnPoner = new JButton(">");
		btnPoner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listPermitidos.getSelectedIndex() >= 0){
					Extension extSelecc= listPermitidos.getSelectedValue();
					modeloSelecc.addElement(extSelecc);
					modeloPerm.removeElement(extSelecc);	
					listPermitidos.setSelectedIndex(0);
					listSelecc.setSelectedIndex(0);
				}
			}
		});
		
		JButton btnSacar = new JButton("<");
		btnSacar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(listSelecc.getSelectedIndex() >= 0){
					Extension extSelecc= listSelecc.getSelectedValue();
					modeloPerm.addElement(extSelecc);
					modeloSelecc.removeElement(extSelecc);
					listPermitidos.setSelectedIndex(0);
					listSelecc.setSelectedIndex(0);
				}				
			}
		});
		
		JButton btnSacarTodo = new JButton("<<");
		btnSacarTodo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int size = modeloSelecc.getSize();
				Extension[] array = new Extension[size];
				modeloSelecc.copyInto(array);
				for(int i = 0; i<array.length;i++){
					modeloPerm.addElement(array[i]);
				}
				modeloSelecc.clear();
				listPermitidos.setSelectedIndex(0);
				listSelecc.setSelectedIndex(0);
			}
		});
		
		JLabel lblFormatosPermitidos = new JLabel("Formatos Permitidos");
		
		JLabel lblFormatosSeleccionados = new JLabel("Formatos Seleccionados");
		GroupLayout gl_panelMedio = new GroupLayout(panelMedio);
		gl_panelMedio.setHorizontalGroup(
			gl_panelMedio.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMedio.createSequentialGroup()
					.addGap(72)
					.addGroup(gl_panelMedio.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelMedio.createSequentialGroup()
							.addComponent(listPermitidos, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addGroup(gl_panelMedio.createParallelGroup(Alignment.LEADING)
								.addComponent(btnPoner)
								.addComponent(btnSacar, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnPonerTodo)
								.addComponent(btnSacarTodo)))
						.addComponent(lblFormatosPermitidos))
					.addGap(9)
					.addGroup(gl_panelMedio.createParallelGroup(Alignment.LEADING)
						.addComponent(lblFormatosSeleccionados)
						.addComponent(listSelecc, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(82, Short.MAX_VALUE))
		);
		gl_panelMedio.setVerticalGroup(
			gl_panelMedio.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMedio.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panelMedio.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblFormatosPermitidos)
						.addComponent(lblFormatosSeleccionados))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panelMedio.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panelMedio.createSequentialGroup()
							.addGap(7)
							.addComponent(btnPoner)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnSacar)
							.addGap(13)
							.addComponent(btnPonerTodo)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnSacarTodo)
							.addGap(24))
						.addComponent(listPermitidos, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
						.addComponent(listSelecc, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(54, Short.MAX_VALUE))
		);
		gl_panelMedio.linkSize(SwingConstants.VERTICAL, new Component[] {btnPonerTodo, btnPoner, btnSacar, btnSacarTodo});
		gl_panelMedio.linkSize(SwingConstants.VERTICAL, new Component[] {listPermitidos, listSelecc});
		gl_panelMedio.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnPonerTodo, btnPoner, btnSacar, btnSacarTodo});
		gl_panelMedio.linkSize(SwingConstants.HORIZONTAL, new Component[] {listPermitidos, listSelecc});
		panelMedio.setLayout(gl_panelMedio);
		
		JPanel panelInferior = new JPanel();
		contentPane.add(panelInferior, BorderLayout.SOUTH);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JButton btnAtras = new JButton("<<");
		btnAtras.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JButton btnSig = new JButton(">>");
		btnSig.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int size =modeloSelecc.size();
				Extension[] ext = new Extension[size];
				modeloSelecc.copyInto(ext);
				if(ext.length ==0){
					JOptionPane.showMessageDialog(contentPane, "Debe seleccionar al menos una Extension", "Error", JOptionPane.ERROR_MESSAGE);
				}else{
					try {
						gestor.setExtensionesSelecc(ext);
						FrameDocumento frame = new FrameDocumento( gestor );
						frame.setLocationRelativeTo(null);
						frame.setVisible(true);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					dispose();
				}
			}
		});
		GroupLayout gl_panelInferior = new GroupLayout(panelInferior);
		gl_panelInferior.setHorizontalGroup(
			gl_panelInferior.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelInferior.createSequentialGroup()
					.addContainerGap(179, Short.MAX_VALUE)
					.addComponent(btnCancelar)
					.addGap(5)
					.addComponent(btnAtras)
					.addGap(5)
					.addComponent(btnSig)
					.addContainerGap())
		);
		gl_panelInferior.setVerticalGroup(
			gl_panelInferior.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelInferior.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panelInferior.createParallelGroup(Alignment.LEADING)
						.addComponent(btnCancelar)
						.addComponent(btnAtras)
						.addComponent(btnSig))
					.addContainerGap())
		);
		gl_panelInferior.linkSize(SwingConstants.VERTICAL, new Component[] {btnCancelar, btnAtras, btnSig});
		gl_panelInferior.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnCancelar, btnAtras, btnSig});
		panelInferior.setLayout(gl_panelInferior);
		
		JLabel lblSeleccionarFormatoDe = new JLabel("Seleccionar Formato de Archivos Permitidos a Analizar:");
		contentPane.add(lblSeleccionarFormatoDe, BorderLayout.NORTH);
	}

	private void settearListaSelecc(DefaultListModel<Extension> modeloPerm) {
		
		
		ArrayList<Extension> extensionesSistema = gestor.extensionesSistema();
		Iterator<Extension> iterador = extensionesSistema.iterator();
		while(iterador.hasNext()){
			modeloPerm.addElement(iterador.next());
		}		
	}
}
